package com.ruoyi.wq.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.Address;
import com.ruoyi.common.utils.ip.AddressUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqComment;
import com.ruoyi.wq.domain.WqCommentReply;
import com.ruoyi.wq.domain.WqDynamic;
import com.ruoyi.wq.domain.WqNoticeComment;
import com.ruoyi.wq.domain.WqNoticeGive;
import com.ruoyi.wq.domain.vo.WqCommentReplyVO;
import com.ruoyi.wq.domain.vo.WqCommentVO;
import com.ruoyi.wq.mapper.WqCommentMapper;
import com.ruoyi.wq.mapper.WqCommentReplyMapper;
import com.ruoyi.wq.mapper.WqDynamicMapper;
import com.ruoyi.wq.mapper.WqNoticeCommentMapper;
import com.ruoyi.wq.service.IWqCommentReplyService;
import com.ruoyi.wq.service.IWqNoticeCommentService;
import com.ruoyi.wq.service.IWqNoticeGiveService;

/**
 * 评论回复Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-29
 */
@Service
public class WqCommentReplyServiceImpl extends ServiceImpl<WqCommentReplyMapper, WqCommentReply> implements IWqCommentReplyService {
    
	@Autowired
    private WqCommentReplyMapper wqCommentReplyMapper;
	
	@Autowired
    private WqCommentMapper wqCommentMapper;
    
    @Autowired
    private WqDynamicMapper wqDynamicMapper;
    
    @Autowired
    private WqNoticeCommentMapper wqNoticeCommentMapper;
    
    @Autowired
    private ISysUserService sysUserService;
    
    @Autowired
    private IWqNoticeCommentService wqNoticeCommentService;
    
    @Autowired
    private IWqNoticeGiveService wqNoticeGiveService;

    /**
     * 查询评论回复
     * 
     * @param id 评论回复主键
     * @return 评论回复
     */
    @Override
    public WqCommentReply selectWqCommentReplyById(String id)
    {
        return wqCommentReplyMapper.selectWqCommentReplyById(id);
    }

    /**
     * 查询评论回复列表
     * 
     * @param wqCommentReply 评论回复
     * @return 评论回复
     */
    @Override
    public TableDataInfo selectWqCommentReplyList(WqCommentReplyVO wqCommentReply){
    	String noticeCommentId = wqCommentReply.getNoticeCommentId();
    	String noticeGiveId = wqCommentReply.getNoticeGiveId();
    	if(StringUtils.isNotBlank(noticeCommentId)){
    		WqNoticeComment notice = wqNoticeCommentMapper.selectById(noticeCommentId);
    		String commentReplyId = notice.getCommentReplyId();
    		wqCommentReply.setId(commentReplyId);
    	}
    	if(StringUtils.isNotBlank(noticeGiveId)){
    		WqNoticeGive notice = wqNoticeGiveService.getById(noticeGiveId);
    		String commentReplyId = notice.getCommentReplyId();
    		if(StringUtils.isNotBlank(commentReplyId)){
    			wqCommentReply.setId(commentReplyId);
    		}
    	}
    	
    	
    	IPage<WqCommentReply> page = Condition.getPage(wqCommentReply);
    	page.setMore(3);
        List<WqCommentReply> replyList = baseMapper.selectPageList(page, wqCommentReply);
        List<WqCommentReplyVO> replyVOList = new ArrayList<WqCommentReplyVO>();
        for(WqCommentReply reply : replyList){
        	WqCommentReplyVO replyVO = new WqCommentReplyVO();
        	BeanUtils.copyProperties(reply, replyVO);
        	replyVO.setTime(DateUtils.getTime(replyVO.getCreateTime()));
        	replyVOList.add(replyVO);
        }
        return Condition.getTableDataInfo(replyVOList, page.getTotal());
    }

    /**
     * 新增评论回复
     * 
     * @param wqCommentReply 评论回复
     * @return 结果
     */
    @Override
    public WqCommentReplyVO insertWqCommentReply(WqCommentReply wqCommentReply) {
    	Long userId = SecurityUtils.getUserId();
    	SysUser sysUser = sysUserService.getById(userId);
    	Address address = AddressUtils.getAddressByIP();
    	
    	wqCommentReply.setUserId(sysUser.getUserId());
    	wqCommentReply.setAvatar(sysUser.getAvatar());
    	wqCommentReply.setNickName(sysUser.getNickName());
    	wqCommentReply.setPhone(sysUser.getPhonenumber());
    	String content = wqCommentReply.getContent();
    	if(StringUtils.getMatch(content)){
    		throw new ServiceException("请文明交友，多次警告会被关进小黑屋哦");
    	}
    	wqCommentReply.setSex(sysUser.getSex());
    	wqCommentReply.setAge(sysUser.getAge());
    	wqCommentReply.setLon(address.getLng());
    	wqCommentReply.setLat(address.getLat());
    	wqCommentReply.setCity(address.getCity());
    	
    	if(wqCommentReply.getReplyUserId() != null){
    		SysUser replyUser = sysUserService.getById(wqCommentReply.getReplyUserId());
        	wqCommentReply.setReplyAvatar(replyUser.getAvatar());
        	wqCommentReply.setReplyNickName(replyUser.getNickName());
        	wqCommentReply.setReplyPhone(replyUser.getPhonenumber());
        	wqCommentReply.setReplySex(replyUser.getSex());
    	}
    	
    	wqCommentReplyMapper.insert(wqCommentReply);
    	
    	// 一级回复通知
    	String commentId = wqCommentReply.getCommentId();
    	WqComment comment = wqCommentMapper.selectById(commentId);
    	WqDynamic dynamic = wqDynamicMapper.selectById(comment.getDynamicId());
    	if(!comment.getUserId().equals(userId)){
    		WqNoticeComment notice = new WqNoticeComment();
        	notice.setDynamicId(dynamic.getId());
        	if(StringUtils.isNotBlank(dynamic.getImage())){
        		notice.setDynamicImage(dynamic.getImage().split(",")[0]);
        	}
        	notice.setDynamicUserId(dynamic.getUserId());
        	
        	notice.setCommentReplyId(wqCommentReply.getId());
        	notice.setCommentId(wqCommentReply.getCommentId());
        	notice.setCommentUserId(comment.getUserId());
        	notice.setCommentNickName(comment.getNickName());
        	
        	notice.setReplyUserId(wqCommentReply.getReplyUserId());
        	notice.setReplyNickName(wqCommentReply.getReplyNickName());
        	
        	notice.setUserId(sysUser.getUserId());
        	notice.setAvatar(sysUser.getAvatar());
        	notice.setNickName(sysUser.getNickName());
        	notice.setSex(sysUser.getSex());
        	notice.setContent(wqCommentReply.getContent());
        	notice.setNoticeUserId(comment.getUserId());
        	wqNoticeCommentMapper.insert(notice);
        	
        	// 更新通知用户未读消息数量
        	SysUser user = sysUserService.getById(comment.getUserId());
        	Integer commentNewNum = wqNoticeCommentService.getUnReadNum(user.getUserId());
        	user.setCommentNewNum(commentNewNum);
        	sysUserService.updateById(user);
    	}
    	
    	// 二级回复通知
    	if(wqCommentReply.getReplyUserId() != null && !wqCommentReply.getReplyUserId().equals(userId)){
    		WqNoticeComment notice = new WqNoticeComment();
    		notice.setDynamicId(dynamic.getId());
    		if(StringUtils.isNotBlank(dynamic.getImage())){
        		notice.setDynamicImage(dynamic.getImage().split(",")[0]);
        	}
    		notice.setDynamicUserId(dynamic.getUserId());
        	
    		notice.setCommentReplyId(wqCommentReply.getId());
    		notice.setCommentId(wqCommentReply.getCommentId());
    		notice.setCommentUserId(comment.getUserId());
    		notice.setCommentNickName(comment.getNickName());
        	
    		notice.setReplyUserId(wqCommentReply.getReplyUserId());
    		notice.setReplyNickName(wqCommentReply.getReplyNickName());
        	
    		notice.setUserId(sysUser.getUserId());
    		notice.setAvatar(sysUser.getAvatar());
    		notice.setNickName(sysUser.getNickName());
    		notice.setSex(sysUser.getSex());
    		notice.setContent(wqCommentReply.getContent());
    		notice.setNoticeUserId(wqCommentReply.getReplyUserId());
        	wqNoticeCommentMapper.insert(notice);
        	
        	// 更新通知用户未读消息数量
        	SysUser user = sysUserService.getById(wqCommentReply.getReplyUserId());
        	Integer commentNewNum = wqNoticeCommentService.getUnReadNum(user.getUserId());
        	user.setCommentNewNum(commentNewNum);
        	sysUserService.updateById(user);
    	}
    	
    	WqCommentReplyVO replyVO = new WqCommentReplyVO();
    	BeanUtils.copyProperties(wqCommentReply, replyVO);
    	replyVO.setTime(DateUtils.getTime(replyVO.getCreateTime()));
        return replyVO;
    }

    /**
     * 修改评论回复
     * 
     * @param wqCommentReply 评论回复
     * @return 结果
     */
    @Override
    public int updateWqCommentReply(WqCommentReply wqCommentReply)
    {
        wqCommentReply.setUpdateTime(DateUtils.getNowDate());
        return wqCommentReplyMapper.updateWqCommentReply(wqCommentReply);
    }

    /**
     * 批量删除评论回复
     * 
     * @param ids 需要删除的评论回复主键
     * @return 结果
     */
    @Override
    public int deleteWqCommentReplyByIds(String[] ids)
    {
        return wqCommentReplyMapper.deleteWqCommentReplyByIds(ids);
    }

    /**
     * 删除评论回复信息
     * 
     * @param id 评论回复主键
     * @return 结果
     */
    @Override
    public int deleteWqCommentReplyById(String id)
    {
        return wqCommentReplyMapper.deleteWqCommentReplyById(id);
    }

	@Override
	public WqCommentVO getReplyListByCommentId(String commentId) {
		WqCommentVO replyPage = new WqCommentVO();
		IPage<WqCommentReply> page = new Page<>(1, 3);
		QueryWrapper<WqCommentReply> queryWrapper = new QueryWrapper<>();
		WqCommentReply entity = new WqCommentReply();
		entity.setCommentId(commentId);
		queryWrapper.setEntity(entity);
		queryWrapper.orderByDesc("create_time");
		page = baseMapper.selectPage(page, queryWrapper);
		List<WqCommentReply> replyList = page.getRecords();
		List<WqCommentReplyVO> replyVOList = new ArrayList<>();
		WqComment comment = wqCommentMapper.selectById(commentId);
		for(WqCommentReply reply : replyList){
			WqCommentReplyVO replyVO = new WqCommentReplyVO();
			BeanUtils.copyProperties(reply, replyVO);
			replyVO.setTime(DateUtils.getTime(replyVO.getCreateTime()));
			if(replyVO.getReplyUserId() == null){
				if(!SecurityUtils.getUserId().equals(replyVO.getUserId()) && !SecurityUtils.getUserId().equals(comment.getUserId())){
					replyVO.setContent("只有回复人和本人可见");
				}
			}else{
				if(!SecurityUtils.getUserId().equals(replyVO.getUserId()) && !SecurityUtils.getUserId().equals(comment.getUserId()) && !SecurityUtils.getUserId().equals(replyVO.getReplyUserId())){
					replyVO.setContent("只有回复人和本人可见");
				}
			}
			replyVOList.add(replyVO);
		}
		replyPage.setReplyList(replyVOList);
		replyPage.setTotal(page.getTotal());
		return replyPage;
	}

	@Override
	public void deleteByCommentId(String commentId) {
		QueryWrapper<WqCommentReply> qw = new QueryWrapper<WqCommentReply>();
		WqCommentReply entity = new WqCommentReply();
		entity.setCommentId(commentId);
		qw.setEntity(entity);
		baseMapper.delete(qw);
	}

	@Override
	public int deleteById(String id) {
		WqCommentReply reply = baseMapper.selectById(id);
		if(!reply.getUserId().equals(SecurityUtils.getUserId())){
			throw new ServiceException("操作失败");
		}
		return baseMapper.deleteById(id);
	}

	@Override
	public WqCommentVO getReplyListByCommentId(String commentId, String commentReplyId) {
		List<WqCommentReplyVO> replyVOList = new ArrayList<>();
		if(commentReplyId != null){
			WqCommentReply reply = baseMapper.selectById(commentReplyId);
			WqCommentReplyVO replyVO = new WqCommentReplyVO();
			BeanUtils.copyProperties(reply, replyVO);
			replyVO.setTime(DateUtils.getTime(replyVO.getCreateTime()));
			replyVOList.add(replyVO);
		}
		
		WqCommentVO replyPage = new WqCommentVO();
		IPage<WqCommentReply> page = new Page<>(1, 3);
		QueryWrapper<WqCommentReply> queryWrapper = new QueryWrapper<>();
		WqCommentReply entity = new WqCommentReply();
		entity.setCommentId(commentId);
		queryWrapper.setEntity(entity);
		if(commentReplyId != null){
			queryWrapper.ne("id", commentReplyId);
		}
		queryWrapper.orderByDesc("create_time");
		page = baseMapper.selectPage(page, queryWrapper);
		List<WqCommentReply> replyList = page.getRecords();
		for(WqCommentReply reply : replyList){
			WqCommentReplyVO replyVO = new WqCommentReplyVO();
			BeanUtils.copyProperties(reply, replyVO);
			replyVO.setTime(DateUtils.getTime(replyVO.getCreateTime()));
			replyVOList.add(replyVO);
		}
		replyPage.setReplyList(replyVOList);
		replyPage.setTotal(page.getTotal());
		return replyPage;
	}
}
